package cn.rongdao.jrkabao.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.dao.VersionDao;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;

/**
 * Created by jiangyongxing on 2017/5/24.
 * 描述：
 */

public class DialogCheckUpdate extends Dialog {

    public DialogCheckUpdate(Context context) {
        super(context);
    }

    public DialogCheckUpdate(Context context, int themeResId) {
        super(context);
    }

    protected DialogCheckUpdate(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {

        /**
         * Helper class for creating a custom dialog
         */

        private Activity activity;
        private String title;


        public Builder(Activity activity) {
            this.activity = activity;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public DialogCheckUpdate.Builder setTitle(int title) {
            this.title = (String) activity.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */
        public DialogCheckUpdate.Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * Create the custom dialog
         */
        public DialogCheckUpdate create() {
            final DialogCheckUpdate dialog = new DialogCheckUpdate(activity, R.style.Dialog);
            dialog.setCanceledOnTouchOutside(false);
            Window window = dialog.getWindow();
            //取消标题
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            //取消状态栏
            window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            window.setBackgroundDrawable(new ColorDrawable());
            window.setContentView(R.layout.dialog_check_update);

            TextView tvTitle = (TextView) window.findViewById(R.id.dcu_tv_title);
            TextView tvContent = (TextView) window.findViewById(R.id.dcu_tv_content);
            View viewLine = window.findViewById(R.id.dcu_view_line);
            TextView tvUpdate = (TextView) window.findViewById(R.id.dcu_tv_update);
            TextView tvCancel = (TextView) window.findViewById(R.id.dcu_tv_cancel);

            tvContent.setMovementMethod(ScrollingMovementMethod.getInstance());

            tvTitle.setText(VersionDao.getInstance().getVersionTitle());
            tvContent.setText(VersionDao.getInstance().getUpdateInfo());

            if (VersionDao.getInstance().isForceUpdate()) {
                dialog.setCancelable(false);
            }

            tvCancel.setVisibility(VersionDao.getInstance().isForceUpdate() ? View.GONE : View.VISIBLE);
            viewLine.setVisibility(VersionDao.getInstance().isForceUpdate() ? View.GONE : View.VISIBLE);

            tvUpdate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!FRString.isEmpty(VersionDao.getInstance().getDownLoadUrl())) {
                        WebViewActivity.start(activity, VersionDao.getInstance().getDownLoadUrl());
                        if (!VersionDao.getInstance().isForceUpdate()) {
                            dialog.dismiss();
                        }
                    }
                }
            });

            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            return dialog;
        }
    }
}